﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using Dict.Config;
using Dict.Blo;
using Dict.DataModel;
using Battle;
using ProtoBuf.Message;

namespace UI
{
    public class PetPage : Page
    {
        /// <summary>
        /// 左侧信息 精灵名字 属性 动画
        /// </summary>
        public UILabel petName;
        public UILabel property;
        public GameObject petAniPos;
        GameObject m_petAni;
        /// <summary>
        /// 属性tab页
        /// </summary>
        public PetInfoTab infoTab;

        /// <summary>
        /// 升级tab页
        /// </summary>
        public PetLevelUpTab levelUpTab;
        /// <summary>
        /// 突破tab页
        /// </summary>
        public PetBreachTab  breachTab;

        /// <summary>
        /// 喂养tab页
        /// </summary>
        public PetFeedTab     feedTab;

        /// <summary>
        /// 右侧tab页按钮
        /// </summary>
        public UIToggle infoTabBtn;
        public UIToggle levelUpTabBtn;
        public UIToggle breachTabBtn;
        public UIToggle feedTabBtn;
        public delegate void OffFormationCallBack(string guid, string type, int pos);
        public OffFormationCallBack m_callback;

        public int curPositions = 0;

        string guid;
        string petId;

        /// <summary>
        /// 宠物动画旋转
        /// </summary>
        Vector3 startDragPos;
        bool isDragging = false;

        /// <summary>
        /// 阵容信息
        /// </summary>
        Dictionary<int, UIHeroFormation> battleCamps = StaticData.playerData.InstPlayerData.BattleCamps;

        void Init()
        {
            UIPet pet = battleCamps[curPositions].pet;
            if (pet == null)
                return;
            guid = pet.guid;
            petId = pet.petId;
            TablePet petData = Dict.DictDataManager.Instance.tablePetDao.GetById("pet_001");
            petName.text = TableTextBlo.GetText("name_"+ petData.id);
            property.text = TableTextBlo.GetText(DictPetBlo.GetPetNature(petData.nature));

            infoTabBtn.GetComponent<UIToggle>().Set(true);
            infoTab.SetPet(guid);
            StopCoroutine("LoadPetAni");
            StartCoroutine(LoadPetAni(petData));
            infoTabBtn.Set(true);
        }
        /// <summary>
        /// 宠物动画
        /// </summary>
        /// <param name="dh"></param>
        /// <returns></returns>
        /// 
        IEnumerator LoadPetAni(Dict.DataModel.TablePet dh)
        {
            yield return StartCoroutine(ResourceLoader.GetInstance().modelLoader.LoadModel(dh.resName, (asset) =>
            {
                GameObject petAni = asset as GameObject;
                if (petAni != null)
                {
                    petAni.name = dh.resName;
                    petAni.transform.parent = petAniPos.transform;
                    petAni.transform.localPosition = Vector3.zero;
                    petAni.transform.localEulerAngles = Vector3.zero;
                    petAni.GetComponent<LegacyAnimation>().Play(AniName.STAND, WrapMode.Loop);
                    petAni.GetComponent<ModelRenderKit>().SetModelScale((float)1);
                    petAni.GetComponent<ModelRenderKit>().SwitchLod(false);
                    m_petAni = petAni;
                }
            }));
            if (m_petAni != null)
            {
                CommonMethod.ChangeLayer(m_petAni.transform, petAniPos.layer);
            }
            else
            {
                Debug.LogError("[PetPage] LoadHeroAni Failed. resName: " + dh.resName);
            }
        }


        public void OnInfoTab()
        {
            Debug.Log("OnInfoTab");
        }

        public void OnLevelUpTab()
        {
            Debug.Log("OnLevelUpTab");
        }

        public void OnBreachTab()
        {
            Debug.Log("OnBreachTab");
            breachTab.SetPet(guid);
        }

        public void OnFeedTab()
        {
            Debug.Log("OnFeedTab");
            feedTab.SetPet(guid);
            feedTab.gameObject.SetActive(true);
        }

        public void OnReplaceBtn()
        {
            Debug.Log("OnReplaceBtn");
        }

        public void OnunloadBtn()
        {
            CSFormationOff msg = new CSFormationOff();
            msg.formatPos = curPositions;
            msg.guid = guid;
            msg.id = petId;
            NetAPI.SendHttp(OpDefine.CSFormationOff, msg, FormationOffCallBack);
        }

        void FormationOffCallBack(NetWork.Packets.Packet packet, bool bSuccess)
        {
            if (bSuccess)
            {
                Close();
            }
        }
        public void OnPetClick()
        {

        }
        public void OnPetDragStart()
        {
            if (m_petAni != null)
            {
                startDragPos = Input.mousePosition;
                isDragging = true;
            }
        }
        public void OnPetDragEnd()
        {
            isDragging = false;
        }
        void Update()
        {
           if(m_petAni != null && isDragging)
           {
               Vector3 v = Input.mousePosition;
               m_petAni.transform.Rotate(new Vector3(0, startDragPos.x - v.x, 0));
               startDragPos = v;

           }
        }

        private UIRoot root;
        private UIRoot GetRoot()
        {
            if (root == null)
            {
                root = NGUITools.FindInParents<UIRoot>(gameObject);
            }
            return root;
        }

        override protected void DoOpen()
        {
           
            gameObject.GetComponent<PageTweener>().Play();
            curPositions = int.Parse(options["index"]);
            Init();
            ////Dict.DataModel.TablePet petdata = Dict.Blo.DictPetBlo.GetTablePet("pet_001");
            ////Dict.Dao.Table2dStoryDao
            //StopCoroutine("LoadPetAni");
            //StartCoroutine(LoadPetAni(petdata));
        }
        protected override void DoClose()
        {
            foreach (Transform tran in petAniPos.transform)
            {
                ResourceLoader.GetInstance().modelLoader.UnloadModel(tran.gameObject, tran.name);
                tran.GetComponent<ModelRenderKit>().SwitchLod(true);
            }
            base.DoClose();
        }

    }

}